<?php
/**
 * @ClassName: Content
 * @Description: TODO
 * @author: wyaohu
 * @Date: 2024/9/8 21:12
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\common\model;
use think\Controller;

class Content extends Controller
{
    /**
     * banner轮播图
     * @return \think\response\Json
     */
    public function banner()
    {
        $data = input();

        $result = $this->validate($data, [
            'position_id|位置ID' => 'require',
        ]);
        if (true !== $result) {
            return json(['code' => 0, 'msg' => $result]);
        }

        $list = model\CmsBanner::where([
            'position_id' => $data['position_id']
        ])->order('sort asc')->select();

        foreach ($list as &$item) {
            $item['image'] = $item['image'] ? replace_url($item['image']) : '';
        }

        return json(['code' => 1, 'data' => $list]);
    }

    /**
     * 获取文章栏目详情
     * @return \think\response\Json
     */
    public function column()
    {
        $id = input('id');

        $data = model\CmsChannel::where('id', $id)->find();

        if ($data) {
            $data['logo'] = replace_url($data['logo']);
        }

        return json(['code' => 1, 'data' => $data]);
    }

    /**
     * 获取栏目文章列表
     * @return \think\response\Json
     */
    public function getColumnArticle()
    {
        $channel_id = input('channel_id', 0);
        $skip = input('skip', 0);
        $limit = input('limit', 10);

        $list = model\CmsContent::where(['channel_id' => $channel_id, 'is_show' => 1])->order('sort asc')->limit($skip, $limit)->select();

        foreach ($list as &$item) {
            $item['logo'] = replace_url($item['logo']);
            $item['content'] = replace_src_url($item['content']);
        }

        $result = [
            'list' => $list,
            'is_more' => count($list) >= $limit
        ];

        return json(['code' => 1, 'msg' => 'ok', 'data' => $result]);
    }

    /**
     * 获取栏目文章详情
     * @return \think\response\Json
     */
    public function getArticle()
    {
        $id = input('id');

        $data = model\CmsContent::where('id', $id)->find();

        if ($data) {
            $data['logo'] = replace_url($data['logo']);
            $data['content'] = replace_src_url($data['content']);
        }

        return json(['code' => 1, 'data' => $data]);
    }

    /**
     * 获取协议
     * @return \think\response\Json
     */
    public function protocol()
    {
        $type = input('type', 'user');

        $data = model\CmsContent::where(['channel_id' => 1, 'keyword' => $type])->find();

        return json(['code' => 1, 'data' => $data]);
    }

}